package com.cnhis.cloudhealth.clinical.hosdoctor.doctororder.bo;

import com.cnhis.cloudhealth.clinical.hosdoctor.doctororder.dao.DoctororderDao;
import com.cnhis.cloudhealth.clinical.hosdoctor.doctorparameter.bo.DoctorparameterBo;
import com.cnhis.cloudhealth.commons.Mappers.ModelVo;
import com.cnhis.cloudhealth.commons.newid.GetNewId;
import com.cnhis.cloudhealth.commons.utils.StaticKeys;

import net.sf.json.JSONObject;

import com.cnhis.cloudhealth.commons.utils.JsonUtil;
import com.cnhis.cloudhealth.commons.utils.MySqlUtil;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * Created by xiezhenyuan on 2016/3/24.
 */
@Service
public class DoctororderBo {
    @Resource
    private DoctororderDao doctororderDao;
    @Autowired
    private DoctorparameterBo doctorparameterBo;
    @Autowired
    GetNewId getNewId;

    public ModelVo selectDOrecord(Map map,int pageno,int pagesize){return MySqlUtil.packModelVoPage("Doctororder.792",map,pageno,pagesize,doctororderDao);}
    public ModelVo selectCLpathwayzy(Map map){return MySqlUtil.packModelVo("Doctororder.7738",map,doctororderDao);}
    public ModelVo selectDOcheck(Map map){return MySqlUtil.packModelVo("Doctororder.1176",map,doctororderDao);}
    public ModelVo selectMechinepercent(Map map){return MySqlUtil.packModelVo("Doctororder.7319",map,doctororderDao);}
    public ModelVo selectSinglePDOinfo(Map map){return MySqlUtil.packModelVo("Doctororder.2004",map,doctororderDao);}
    public ModelVo selectRPuserinfo(Map map){return MySqlUtil.packModelVo("Doctororder.1133",map,doctororderDao);}
    public ModelVo selectMedicineallergy(Map map){return MySqlUtil.packModelVo("Doctororder.7283",map,doctororderDao);}
    public ModelVo countNotprintrows(Map map){return MySqlUtil.packModelVo("Doctororder.2115",map,doctororderDao);}
    public ModelVo countLastpageno(Map map){return MySqlUtil.packModelVo("Doctororder.2133",map,doctororderDao);}
    public ModelVo countPrintpageno(Map map){return MySqlUtil.packModelVo("Doctororder.2138",map,doctororderDao);}
    public ModelVo countNotprintpageno(Map map){return MySqlUtil.packModelVo("Doctororder.4179",map,doctororderDao);}
    public ModelVo checkxuda(Map map){return MySqlUtil.packModelVo("Doctororder.7737",map,doctororderDao);}
    public ModelVo selectExerecord(Map map){
        return MySqlUtil.packModelVo("Doctororder.1019",map,doctororderDao);
    }
    public ModelVo selectNursingOrder(){return MySqlUtil.packModelVo("Doctororder.1320",null,doctororderDao);}
    public ModelVo selectTransferDep(Map map){return MySqlUtil.packModelVo("Doctororder.1267",map,doctororderDao);}
    public ModelVo selectOutOrder(){return MySqlUtil.packModelVo("Doctororder.1318",null,doctororderDao);}
    public ModelVo selectTransferOrder(){return MySqlUtil.packModelVo("Doctororder.zhuankeyizhu",null,doctororderDao);}
    public ModelVo selectHuizhenOrder(){return MySqlUtil.packModelVo("Doctororder.huizhenyizhu",null,doctororderDao);}
    public ModelVo hasCheckbody(Map map){return MySqlUtil.packModelVo("Doctororder.1452",map,doctororderDao);}
    public ModelVo selectCheckbody(Map map){return MySqlUtil.packModelVo("Doctororder.1456",map,doctororderDao);}
    public ModelVo selectCheckmethod(Map map){return MySqlUtil.packModelVo("Doctororder.1765",map,doctororderDao);}
    public ModelVo selectHisCndrug(Map map){return MySqlUtil.packModelVo("Doctororder.selectHisCndrug",map,doctororderDao);}
    public ModelVo selectPischildbirth(Map map){return MySqlUtil.packModelVo("Doctororder.1256",map,doctororderDao);}
    public ModelVo selectApparatus (){return MySqlUtil.packModelVo("Doctororder.11338",null,doctororderDao);}
    public ModelVo selectBookinfo (Map map){return MySqlUtil.packModelVo("Doctororder.11339",map,doctororderDao);}
    public ModelVo selectIsBooking (Map map){return MySqlUtil.packModelVo("Doctororder.11344",map,doctororderDao);}
    public ModelVo selectOporderdep(Map map){return MySqlUtil.packModelVo("Doctororder.oporderdep",map,doctororderDao);}
    public ModelVo selectNeonatalrecords (Map map){return MySqlUtil.packModelVo("Doctororder.1253",map,doctororderDao);}

    public ModelVo selectCLpathway(Map map){
        ModelVo mo =  MySqlUtil.packModelVo("Doctororder.7703_001",map,doctororderDao);
        Long result = doctororderDao.getLongValue(mo);
        map.put("BIG01",result);
        return MySqlUtil.packModelVo("Doctororder.7703_002",map,doctororderDao);
    }
    @SuppressWarnings("unchecked")
    public ModelVo selectNSplan(Map map){
        ModelVo mo =  MySqlUtil.packModelVo("Doctororder.7751_001",map,doctororderDao);
        Long result = doctororderDao.getLongValue(mo);
        map.put("BIG01",result);
        return MySqlUtil.packModelVo("Doctororder.7751_002",map,doctororderDao);
    }
    @SuppressWarnings("unchecked")
    public ModelVo nurseOrderStopDoubt(Map map)throws Exception{
       Long lVAF01 = MapUtils.getLong(map,"lVAF01");
       Integer lBDA01 = MapUtils.getInteger(map,"lBDA01");
       Long VAF01A = lVAF01;

       Long cn =  doctororderDao.queryForLong("Doctororder.nos_001",map);
       if(cn>0){
           return new ModelVo(StaticKeys.OH_FALSE, "该医嘱记录状态已变，请刷新后操作！");
       }

        if(lBDA01>=1&&lBDA01<=2){
            Long vafa = (Long)doctororderDao.queryToObject("Doctororder.nos_001",map);
            if(vafa!=null){
                VAF01A = vafa;
            }
        }
        if(VAF01A!=null) {
            //更新医嘱记录表
            doctororderDao.update("Doctororder.nos_003", map);
            doctororderDao.update("Doctororder.nos_004", map);
            Long VBG01 = getNewId.nextId();
            map.put("VBG01", VBG01);
            map.put("lDate", new Date());
            //插入医嘱状态
            doctororderDao.insert("Doctororder.nos_005", map);
            return new ModelVo(StaticKeys.OH_SUCCESS, "更新成功！");
        }else{
            return new ModelVo(StaticKeys.OH_FALSE, "医嘱记录为空！");
        }
    }

    @SuppressWarnings("unchecked")
    public ModelVo selectNewbornsearch(Map map){
        Integer VAA07 = MapUtils.getInteger(map,"VAA07",0);
        Integer VAP01  = MapUtils.getInteger(map,"VAP01",0);
        if(VAA07==0&&VAP01>0){
            return MySqlUtil.packModelVo("Doctororder.1256_001",map,doctororderDao);
        }else{
            return MySqlUtil.packModelVo("Doctororder.1256_002",map,doctororderDao);
        }
    }

    @SuppressWarnings("unchecked")
    public ModelVo updateNewbornsign (Map map)throws Exception{
        doctororderDao.update("Doctororder.7696",map);
        return new ModelVo(StaticKeys.OH_SUCCESS, "更新成功！");
    }
    @SuppressWarnings({ "unchecked" })
    public ModelVo selectIsfeedestroy (Map map)throws Exception{
    	 ModelVo mo = new ModelVo();
    	 mo.setResult(StaticKeys.OH_SUCCESS);
         List<Long> list = null;
         String lxml = MapUtils.getString(map,"lxml"); //获取医嘱相关json信息字符串
         JSONObject job = JSONObject.fromObject(lxml);
         if(job==null||job.isEmpty()){
             return new ModelVo(StaticKeys.OH_FALSE, "提交信息错误，医嘱相关信息格式不正确，请检查！");
         }
         if (!job.containsKey("cbmrow")) {
             return mo;
         }
         List<Map<String,Object>> listrow = JsonUtil.parserToList(job.getJSONArray("cbmrow"));
         for(int i=0;i<listrow.size();i++){
        	 Map map1 = listrow.get(i);
        	 Long cn = (Long)doctororderDao.queryToObject("Doctororder.6423_001",map1);
             Long vaj01 = 0l;
             if(cn>0){
            	 vaj01 = (Long)doctororderDao.queryToObject("Doctororder.6423_002",map1);
             }else{
            	 vaj01 = (Long)doctororderDao.queryToObject("Doctororder.6423_003",map1);
             }
             if(vaj01!=0){
            	 list.add(vaj01);
            	 mo.setList(list);
                 mo.setResult(StaticKeys.OH_SUCCESS);
                 mo.setTotal(list.size()); 
                 return mo;
             }             
         }          
        return mo;
    }
    @SuppressWarnings("unchecked")
    public ModelVo hideScreenPrint (Map map)throws Exception{
    	 ModelVo mo = new ModelVo();
    	 mo.setResult(StaticKeys.OH_SUCCESS);
         List<Long> list = null;
         String lxml = MapUtils.getString(map,"lxmls"); //获取医嘱相关json信息字符串
         JSONObject job = JSONObject.fromObject(lxml);
         if(job==null||job.isEmpty()){
             return new ModelVo(StaticKeys.OH_FALSE, "提交信息错误，医嘱相关信息格式不正确，请检查！");
         }
         if (!job.containsKey("cbmrow")) {
             return mo;
         }
         List<Map<String,Object>> listrow = JsonUtil.parserToList(job.getJSONArray("cbmrow"));
         for(int i=0;i<listrow.size();i++){
        	 Map map1 = listrow.get(i);
        	 Long cn = (Long)doctororderDao.queryToObject("Doctororder.4184_001",map1);
             if(cn>0){
                 doctororderDao.update("Doctororder.4184_002",map1);
             }else{
                 doctororderDao.update("Doctororder.4184_003",map1);
             }            
         }     
        
        return new ModelVo(StaticKeys.OH_SUCCESS, "更新成功！");
    }
    //医嘱审核
    @SuppressWarnings("unchecked")
    public ModelVo ClinicOrderAudit (Map map)throws Exception{
    	 ModelVo mo = new ModelVo();
    	 mo.setResult(StaticKeys.OH_SUCCESS);
    	 String lbce03 = MapUtils.getString(map,"lbce03");
         String lxml = MapUtils.getString(map,"lxmls"); //获取医嘱相关json信息字符串
         JSONObject job = JSONObject.fromObject(lxml);
         if(job==null||job.isEmpty()){
             return new ModelVo(StaticKeys.OH_FALSE, "提交信息错误，医嘱相关信息格式不正确，请检查！");
         }
         if (!job.containsKey("vafrow")) {
             return mo;
         }
         List<Map<String,Object>> listrow = JsonUtil.parserToList(job.getJSONArray("vafrow"));
         map.put("listrow", listrow);
         mo = MySqlUtil.packModelVo("Doctororder.selectTmpvaf",map,doctororderDao);
         List<Map<String,Object>> tmpvaf = (List<Map<String, Object>>) mo.getList();
         if(tmpvaf == null || tmpvaf.size()==0){
				return new ModelVo(StaticKeys.OH_FALSE, "提取对应医嘱信息错误！");
         }
         for (Map<String, Object> mm : tmpvaf) {
 			mm.put("vbg01", getNewId.nextId());
 			mm.put("bce03", lbce03);
 		 }
         map.put("listrow", tmpvaf);
         doctororderDao.insert("Doctororder.inservbg",map);
        return new ModelVo(StaticKeys.OH_SUCCESS, "审核成功！");
    }
    //Nurse_VBV1_Print_OK  续打存储过程
    public ModelVo insertVBV1print(Map<String,Object> map)throws Exception{
        String svbv04 = MapUtils.getString(map,"svbv04");
        String vbv04s[] = svbv04.split(",");
        int vbv04size = vbv04s.length;
        List<Map<String, Object>> vbv04list = new ArrayList<Map<String, Object>>();
        for(int i=0;i<vbv04size;i++){
        	Map<String, Object> map1 = new HashMap<String, Object>();
        	map1.put("vbv04", vbv04s[i]);
        	vbv04list.add(map1);
        }
        map.put("vbv04list", vbv04list);
        int count1 = (int)doctororderDao.queryForExtObject("Doctororder.count_1",map);
        if(count1<1){
        	return new ModelVo(StaticKeys.OH_FALSE, "选择页码不对，此页码对应医嘱单可能还没打印!");
        }
        int count2 = (int)doctororderDao.queryForExtObject("Doctororder.count_2",map);
        if(count2<vbv04size){
        	return new ModelVo(StaticKeys.OH_FALSE, "输入行号不对，此页码还不存在对应行号!");
        }
        doctororderDao.update("Doctororder.print_ok",map);
    	return new ModelVo(StaticKeys.OH_SUCCESS, "医嘱续打插入成功");
    }


    //Clinic_VBV1_Insert 病人医嘱打印记录插入
    public ModelVo insertVBV1(Map<String,Object> map)throws Exception{
        Integer lNew = MapUtils.getInteger(map,"lNew");
        Long lVAF06 = MapUtils.getLong(map,"lVAE01");
        Long lVAF07 = MapUtils.getLong(map,"lVAF07");
        Integer lVAF11 = MapUtils.getInteger(map,"lVAF11");
        Integer lSign = 0;
        Integer lCount = MapUtils.getInteger(map,"lCount");
        String lBCE03 = MapUtils.getString(map,"lBCE03");

        String oneRow = doctorparameterBo.getSysValue(9999,9999,13);    //医嘱单打印，用法位置在医嘱后面，不单独一行
        String para257 = doctorparameterBo.getSysValue(100,105001,257); //重整医嘱续打换页
        String para369 = doctorparameterBo.getSysValue(100,105001,369); //转科医嘱续打换页
        String para370 = doctorparameterBo.getSysValue(100,105001,370); //术后医嘱续打换页
        if(lNew==1){
            doctororderDao.delete("Doctororder.ivbv_001",map);
        }
        List<Map<String,Object>> kbmVAF = doctororderDao.queryForList("Doctororder.ivbv_002",map);
        Map<String,Object> mVaf1 = new HashMap<String,Object>();
        List<Map<String,Object>> tVaf = new ArrayList<Map<String,Object>>();
        Iterator<Map<String,Object>> it = kbmVAF.iterator();
        int i = 1;
        
        while(it.hasNext()){
            Map<String,Object> vaf = it.next();
            Long cbm01 = MapUtils.getLong(vaf,"cbm01");
            Long vaf01 = MapUtils.getLong(vaf,"vaf01");
            Integer vaf59 = MapUtils.getInteger(vaf,"vaf59");
            Integer rownr = MapUtils.getInteger(vaf,"rowm");
            Long vaf06 = MapUtils.getLong(vaf,"vaf06");
            Long vaf07 = MapUtils.getLong(vaf,"vaf07");
            Integer vaf04 = MapUtils.getInteger(vaf,"vaf04");
            Integer vaf11 = MapUtils.getInteger(vaf,"vaf11");
            Integer vaf54 = MapUtils.getInteger(vaf,"vaf54");
            String bda01 = MapUtils.getString(vaf,"bda01");
            String vaf36 = MapUtils.getString(vaf,"vaf36");
            String vaf01c = MapUtils.getString(vaf,"vaf01c");
            String vaf74 = MapUtils.getString(vaf,"vaf74");
            Long vaf01a = MapUtils.getLong(vaf,"vaf01a");
            Integer vaf32 = MapUtils.getInteger(vaf,"vaf32");
            String bbx13 = "0";

            String key = cbm01+"_"+vaf59;
            boolean flag = false;
            if(vaf06.longValue()==lVAF06.longValue()
                    &&vaf07.longValue()==lVAF07.longValue()
                    &&vaf04.intValue()==2
                    &&vaf11.intValue()==lVAF11.intValue()
                    &&vaf54.intValue()==0
                    &&("1".equals(bda01)||"2".equals(bda01))) {
                if (mVaf1.get(key) != null) {
                    Integer rn = (Integer) mVaf1.get(key);
                    if (rownr.intValue() > rn.intValue()) {
                        mVaf1.put(key, rownr.intValue());
                    }
                } else {
                    mVaf1.put(key, rownr.intValue());
                }
                flag = true;

            }else if((("8".equals(bda01)||"S".equals(bda01)||("E".equals(bda01)))
                     &&vaf01a.longValue()==0l
                     &&vaf06.longValue()==lVAF06.longValue()
                     &&vaf07.longValue()==lVAF07.longValue()
                     &&vaf04.intValue()==2
                     &&vaf11.intValue()==lVAF11.intValue()
                     &&vaf54.intValue()==0)){
                flag = true;
            }else if(!(bda01.equals("1")&&bda01.equals("2")&&bda01.equals("3")&&bda01.equals("8")&&bda01.equals("S")&&bda01.equals("E"))
                    &&vaf01a.intValue()==0
                    &&vaf32.intValue()==0
                    &&vaf54.intValue()==0
                    &&vaf06.longValue()==lVAF06.longValue()
                    &&vaf07.longValue()==lVAF07.longValue()
                    &&vaf04.intValue()==2
                    &&vaf11.intValue()==lVAF11.intValue()){
                if("Z".equals(bda01)){
                    String b13 = (String)doctororderDao.queryForExtObject("Doctororder.ivbv_003",map);
                    if(b13!=null){
                        bbx13 = b13;
                    }
                }
                flag = true;
            }else if("T".equals(bda01)
                    &&0l==vaf01a.longValue()
                    &&vaf04.intValue()==2
                    &&vaf11.intValue()==lVAF11.intValue()
                    &&vaf06.longValue()==lVAF06.longValue()
                    &&vaf07.longValue()==vaf07.longValue()
                    &&vaf54.intValue()==0
                    &&vaf32.intValue()==1 && "0".equals(oneRow)){
                flag = true;
            }
            if(flag){
                Map<String,Object> tmap = new HashMap<String,Object>();
                tmap.put("id",i);
                tmap.put("cbm01",cbm01);
                tmap.put("vaf59",vaf59);
                tmap.put("rownr",rownr);
                tmap.put("vaf01",vaf01);
                tmap.put("vaf36",vaf36);
                tmap.put("vaf01c",vaf01c);
                tmap.put("vaf74",vaf74);
                tmap.put("bda01",bda01);
                tmap.put("bbx13",bbx13);
                tVaf.add(tmap);
                i++;
            }
        }
       

        List<Map<String,Object>> tmpVBV = doctororderDao.queryForList("Doctororder.ivbv_004",map);
        if((tmpVBV!=null&&tmpVBV.size()>0)&&(tVaf==null||tVaf.size()==0)){
            lSign = 1;
        }else{
            Iterator<Map<String,Object>> it4 = tmpVBV.iterator();
            
            int x = 0;
            while(it4.hasNext()){
                Map<String,Object> o1 = it4.next();
                Long vaf01o = MapUtils.getLong(o1,"vaf01");
                Iterator<Map<String,Object>> it5 = tVaf.iterator();
                while(it5.hasNext()){
                    Map<String,Object> o2 = it5.next();
                    Long vaf01i = MapUtils.getLong(o2,"vaf01");
                    if(vaf01o.longValue()==vaf01i.longValue()){
                    	x++;
                    }
                }
            }
            if(x<tVaf.size()){
                lSign = 1;
            }else{
            	return new ModelVo(StaticKeys.OH_SUCCESS, "医嘱打印插入成功");
            }
        }

        Long lVAA01 = (Long)doctororderDao.queryForExtObject("Doctororder.ivbv_005",map);
        map.put("lVAA01",lVAA01);
        if(lSign==1){
            doctororderDao.delete("Doctororder.ivbv_001",map);
        }


        Integer lVBV04 = 0;
        Integer lVBV03M = 0;
        lVBV03M = (Integer)doctororderDao.queryForExtObject("Doctororder.ivbv_006",map);
        map.put("lVBV03M",lVBV03M);
        if(lVBV03M!=null&&lVBV03M.intValue()==0){
            lVBV04 = 0;
            lVBV03M = 1;
        }else{
            lVBV04 = (Integer)doctororderDao.queryForExtObject("Doctororder.ivbv_007",map);
        }
        if(tVaf!=null&&tVaf.size()>0) {
            Iterator<Map<String, Object>> it6 = tVaf.iterator();
            boolean flag2 = false;
            while(it6.hasNext()){
                Map<String,Object> mo = it6.next();
                String bda01 = MapUtils.getString(mo,"bda01");
                String bbx13 = MapUtils.getString(mo,"bbx13");
                if(("Z".equals(bda01)&&"13".equals(bbx13)&&"1".equals(para257))
                        ||("Z".equals(bda01)&&"5".equals(bbx13)&&"1".equals(para369))
                        ||("Z".equals(bda01)&&"6".equals(bbx13)&&"1".equals(para370))){
                    flag2 = true;
                }
                if(flag2){
                    lVBV03M = lVBV03M + 1;
                    lVBV04 = 1;
                }else{
                    lVBV04 = lVBV04 + 1;
                    if(lVBV04.intValue()>lCount.intValue()){
                        lVBV03M = lVBV03M + 1;
                        lVBV04 = 1;
                    }
                }


                mo.put("lVBV03M",lVBV03M);
                mo.put("lVBV04",lVBV04);
                Long vaf01l = MapUtils.getLong(mo,"vaf01");
                while(it.hasNext()){
                    Map<String,Object> vff = it.next();
                    Long vaf01k = MapUtils.getLong(vff,"vaf01");
                    if(vaf01l.longValue()==vaf01k.longValue()) {
                        Integer vaf10 = MapUtils.getInteger(vff, "vaf10");
                        Integer vbv14 = 0;
                        Date vbv15 = null;
                        String vbv16 = "";
                        Integer vbv17 = 0;
                        Date vbv18 = null;
                        String vbv19 = "";
                        if (vaf10 == 8 || vaf10 == 9) {
                            vbv14 = 1;
                            vbv15 = new Date();
                            vbv16 = lBCE03;
                            if (vaf10 == 9) {
                                vbv17 = 1;
                                vbv18 = new Date();
                                vbv19 = lBCE03;
                            }
                        } else {
                            vbv14 = 0;
                        }
                        mo.put("vbv14",vbv14);
                        mo.put("vbv15",vbv15);
                        mo.put("vbv16",vbv16);
                        mo.put("vbv17",vbv17);
                        mo.put("vbv18",vbv18);
                        mo.put("vbv19",vbv19);
                    }

                }
                Long lVBV01 = getNewId.nextId();
                mo.put("lVBV01",lVBV01);
            }
            
            map.put("tmpVBV",tVaf);

            doctororderDao.insert("Doctororder.ivbv_008",map);

        }
       return new ModelVo(StaticKeys.OH_SUCCESS, "医嘱打印插入成功");
    }
}
